<?


class db {

	function Connect()
	{
		if(!defined('DBSERVER')) die("Database Environment vars not defined.");
		@mysql_connect(DBSERVER,DBUSER,DBPASS) or die("En estos momentos nos encontramos en mantenimiento. Por favor intente mas tarde.");
		mysql_select_db(DBNAME) or die("No se puede seleccionar la base de datos");
		return true;
	}
	
	function CrearBusquedaCampos($campos,$busqueda, $excepciones = NULL) {
		$separador = "or";
		$nuevo = array();
		$esarreglo = is_array($excepciones);		
		foreach($campos as $campofull) {
			$campo = explode(" ",$campofull);
			$procesar = true;
			if($esarreglo) if(in_array($campo[0],$excepciones)) $procesar = false;
			if($procesar) $nuevo[]=$campo[0]." ilike '%".htmlspecialchars($busqueda)."%'";
		}
		return "(".implode(" ".$separador." ",$nuevo).")";
	}
	
	function GenerateCombo($query,$selected = 0, $titulo = "-- Seleccione --") { 
		$result = mysql_query($query);
		$combo = '<option value="0">'.$titulo.'</option>';		
		if($titulo=="") $combo="";
		if($result && mysql_num_rows($result)>0)
		{
			$n = mysql_num_rows($result);				
			while($row = mysql_fetch_array($result)) {
				$sel ="";								
				if($selected!=-1) if(strval($selected)==strval($row[0]) || $n==1) $sel = ' selected="selected"';
				//echo($selected." ".$row[0]."<br>");
				$combo=$combo.'<option value="'.$row[0].'"'.$sel.'>'.$row[1].'</option>';
			}
		}
		return utf8_decode($combo);
	}
	
	
	function get($campo) {
		return db::ExecuteScalar("SELECT content FROM settings WHERE name = '$campo' LIMIT 1");				
	}
	
	function set($campo,$valor) {
		db::saveconfig($campo,$valor);	
	}
	
	function readconfig($campo) {
		return db::ExecuteScalar("SELECT content FROM settings WHERE name = '$campo'");		
	}

	function saveconfig($campo, $valor) {
		$result = mysql_query("SELECT * FROM settings WHERE name = '$campo'");		
		if($result && mysql_num_rows($result)>0)
		{
			$query = "UPDATE settings SET content = '".$valor."' WHERE name = '$campo'";
		} else { 
			$query = "INSERT INTO settings (name,content) values ('".$campo."','".$valor."')";
		}
		mysql_query($query);
	}
	
	function ExecuteScalar($query, $isnumber = false)
	{
		//echo($query);
		if($result = mysql_query($query)) 
		{
			if($row = mysql_fetch_array($result)) {
				return $row[0];
			}
		}
		if($isnumber) return 0; else return "";
	}
	
	function getScalar($query) {
		if($result=mysql_query($query)) {
			if($row=mysql_fetch_array($result)) {
				return $row[0];
			}
		}
		return "";
	}
	
	function getList($query, $usarcomillas = false) {
		if($result = mysql_query($query)) {			
			$arr = array();
			if(mysql_num_rows($result)>0) { 				
				while($fila = mysql_fetch_array($result)) {
					$arr[]=$fila[0];
				}			
				$res = ($usarcomillas?"'".implode("','",$arr)."'":implode(",",$arr));
			}
		}	
		return $res;
	}
	

	function getRow($query)
	{
		$result = mysql_query($query);
		if($result) 
		{
			if($row = mysql_fetch_assoc($result)) {
				return $row;
			}
		}
		return array();
	}
	

	function Exists($query)
	{
		$result = mysql_query($query);		
		return (mysql_num_rows($result)>0);
	}
	
	function getArray($query,$field = 0) // returns an array with field from sql query
	{
		$ret = array();
		$result = mysql_query($query);
		if(mysql_num_rows($result)>0) while ($row = mysql_fetch_array($result)) $ret[] = $row[$field];		
		return $ret;
	}
	
	function getArrayKeyValue($query,$key,$value) // returns an array with field from sql query
	{
		$ret = array();
		$result = mysql_query($query);
		if(mysql_num_rows($result)>0) while ($row = mysql_fetch_assoc($result)) $ret[$row[$key]] = utf8_decode($row[$value]);		
		return $ret;
	}
	
	function getArrayKey($query) // returns an array with field from sql query
	{
		$ret = array();
		$result = @mysql_query($query);
		if(!$result) die("error: ".$query);
		if(mysql_num_rows($result)>0) while ($row = mysql_fetch_array($result)) $ret[$row[0]] = $row[1];		
		return $ret;
	}
		
	function SearchText($str,$table)
	{
		if(!is_array($table)) $table = array($table);
		if(!is_array($str)) $str = array($str);
		$retarray = array();
		$excepciones = array("fecha_","id_","hora_","precio","destacado","aprobado","password","rating");
		foreach($table as $t) {
			$result = mysql_query('DESCRIBE '.$t);
			if(mysql_num_rows($result)>0) {
				while ($row = mysql_fetch_array($result)) {
					foreach($str as $string) {
						$agregar = true;
						foreach($excepciones as $e) {
							if(strpos($row[0],$e)!==false) $agregar=false;
						}
						if($agregar) $retarray[] = $t.".".$row[0]." LIKE '%".$string."%'";
					}
				}
			}
		}
		if(count($retarray)>0) return "(".implode(' OR ',$retarray).")"; else return "";
	}
	
	
	function Close() {
		mysql_close();	
	}

}



?>